//
//  ForgetPwdVC.swift
//  RPay
//
//  Created by Charlie Liao on 2017/6/27.
//  Copyright © 2017年 Charlie. All rights reserved.
//

class ForgetPwdVC: BaseVC, CTAPIManagerParamSource, CTAPIManagerCallBackDelegate {
    
    // MARK: - Init
    convenience init() {
        self.init(nibName: "ForgetPwdView", bundle: nil)
    }
    
    // MARK: - Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocalStr("忘记密码")
        
    }
    
    // MARK: - CTAPIManagerParamSource
    
    func params(forApi manager: CTAPIBaseManager!) -> [AnyHashable : Any]!
    {
        if manager == _codeApi {// 获取验证码
            return [kApiParamUserName: userTF.text!]
        }
        else if manager == _updateApi { // 修改密码
            return [kApiParamUserName: userTF.text!, kApiParamUserPwd: pwdTF.text!, kApiParamVerifyCode: codeTF.text!]
        }
        
        return [AnyHashable : Any]()
    }
    
    // MARK: - CTAPIManagerCallBackDelegate
    
    func managerCallAPIDidSuccess(_ manager: CTAPIBaseManager!)
    {
        hideLoading()
        if manager == _codeApi { // 获取验证码
            _codeApi = nil
        }
        else if manager == _updateApi { // 修改密码
            _updateApi = nil
            self.navigationController!.alert(LocalStr("密码修改成功"))
            self.navigationController!.popViewController(animated: true)
        }
    }
    
    func managerCallAPIDidFailed(_ manager: CTAPIBaseManager!) {
        hideLoading()
        alert(manager.errorMessage)
        if manager == _codeApi {
            _codeApi = nil
        } else if manager == _updateApi {
            _updateApi = nil
        }
    }
    
    // MARK: - Event respond
    
    /// 点击获取验证码按钮
    @IBAction func tapGetCodeBtn() {
        if userTF.text!.isEmpty {
            alert(LocalStr("请输入登录账号"))
            return
        }
        showLoading()
        codeApi.loadData()
    }
    
    /// 点击完成按钮
    @IBAction func tapSubimtBtn() {
        if userTF.text!.isEmpty {
            alert(LocalStr("请输入登录账号"))
            return
        }
        if pwdTF.text!.isEmpty {
            alert(LocalStr("请输入新密码"))
            return
        }
        if pwdTF.text! != pwd2TF.text! {
            alert(LocalStr("输入的两次新密码不一致"))
            return
        }
        
        if pwdTF.text!.isEmpty {
            alert(LocalStr("请输入验证码"))
            return
        }
        
        showLoading()
        updateApi.loadData()
    }
    
    @IBOutlet private var userTF: UITextField!
    @IBOutlet private var pwdTF: UITextField!
    @IBOutlet private var pwd2TF: UITextField!
    @IBOutlet private var codeTF: UITextField!
    
    
    // MARK: - Get
    
    private var _codeApi: GetCodeApiManager?
    private var codeApi: GetCodeApiManager {
        if _codeApi == nil {
            _codeApi = GetCodeApiManager()
            _codeApi!.paramSource = self
            _codeApi!.delegate = self
        }
        return _codeApi!
    }
    
    private var _updateApi: UpdatePwdApiManager?
    private var updateApi: UpdatePwdApiManager {
        if _updateApi == nil {
            _updateApi = UpdatePwdApiManager()
            _updateApi!.paramSource = self
            _updateApi!.delegate = self
        }
        return _updateApi!
    }
}
